<?php


namespace app\system\service;

class DynamicService
{
	public static function getlist($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["status"]) && $options["status"] != "") {
			$where[] = ["status", "=", $options["status"]];
		}
		if (!empty($options["cid"])) {
			$where[] = ["cid", "=", $options["cid"]];
		}
		if (!empty($options["carry_video"])) {
			$where[] = ["carry_video", "=", 1];
		}
		if (!empty($options["carry_image"])) {
			$where[] = ["carry_image", "=", 1];
		}
		if (!empty($options["label_name"])) {
			$label = \think\Db::name("app_member_dynamic_label")->where(["appid" => $options["appid"], "name" => $options["label_name"]])->find();
			if (empty($label)) {
				$where[] = ["id", "=", 0];
			} else {
				$where[] = ["", "exp", \think\Db::raw("FIND_IN_SET({$label["id"]},label_ids)")];
			}
		}
		if (isset($options["keyword"]) && $options["keyword"]) {
			$where[] = ["", "exp", \think\Db::raw("(title like '%{$options["keyword"]}%' or content like '%{$options["keyword"]}%' )")];
		}
		$count = \think\Db::name("app_member_dynamic")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_member_dynamic")->where($where)->page($options["page"], $options["limit"])->order("id", "desc")->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_dynamic_array($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_dynamic_array($item)
	{
		$user = \think\Db::name("app_member_user")->where(["id" => $item["uid"]])->find();
		$media_file_list = [];
		$media_file_ids = json_decode($item["media_file_ids"], true) ?: [];
		foreach ($media_file_ids as $media_file_id) {
			$file = \think\Db::name("public_upload_log")->where(["id" => $media_file_id])->find();
			if (!empty($file)) {
				if (empty($file["channel"])) {
					if ($file["type"] == 0) {
						$file["url"] = get_static_resources_url($file["uri"]);
					} elseif ($file["type"] == 1) {
						$file["url"] = get_static_resources_url($file["uri"], 1);
					} elseif ($file["type"] == 2) {
						$file["url"] = get_static_resources_url($file["uri"], 2);
					}
				}
				$media_file_list[] = $file;
			}
		}
		$label_ids = \think\Db::name("app_member_dynamic_label_ids")->where(["dynamic_id" => $item["id"]])->column("label_id");
		$label_list = \think\Db::name("app_member_dynamic_label")->whereIn("id", $label_ids)->field("id,name")->select();
		$data = ["id" => $item["id"], "uid" => $item["uid"], "cid" => $item["cid"], "classify_info" => \think\Db::name("app_member_dynamic_classify")->where("id", $item["cid"])->field("id,name")->find(), "title" => $item["title"], "content" => $item["content"], "addtime" => $item["addtime"], "status" => $item["status"], "hidden_content" => $item["hidden_content"], "get_hidden_content_mode" => $item["get_hidden_content_mode"], "get_hidden_content_mode_point_quantity" => $item["get_hidden_content_mode_point_quantity"], "media_file_list" => $media_file_list, "user_info" => ["id" => $user["id"], "avatar" => $user["avatar"], "nickname" => $user["nickname"]], "likeNumber" => \think\Db::name("app_member_dynamic_like_log")->where(["dynamic_id" => $item["id"]])->count(), "collectNumber" => \think\Db::name("app_member_dynamic_collect_log")->where(["dynamic_id" => $item["id"]])->count(), "browseUserNumber" => \think\Db::name("app_member_dynamic_browse_log")->where(["dynamic_id" => $item["id"]])->count(), "browseNumber" => $item["browse"], "chatNumber" => \think\Db::name("app_member_dynamic_comment")->where(["dynamic_id" => $item["id"]])->count(), "label_list" => $label_list ?: [], "carry_image" => $item["carry_image"], "carry_video" => $item["carry_video"]];
		$share_file_list = json_decode($item["share_file_list"]) ?: [];
		$data["share_file_list"] = $share_file_list;
		return $data;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_member_dynamic")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "动态ID不存在"];
		}
		\think\Db::name("app_member_dynamic")->where(["id" => $item["id"]])->setInc("browse", 1);
		return ["code" => \app\system\model\Code::success, "data" => self::get_dynamic_array($item)];
	}
	public static function get_comment_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["status"]) && $options["status"] != "") {
			$where[] = ["status", "=", $options["status"]];
		}
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["pid"]) && $options["pid"] != "") {
			$where[] = ["pid", "=", $options["pid"]];
		}
		if (isset($options["reply_id"]) && $options["reply_id"] != "") {
			$where[] = ["reply_id", "=", $options["reply_id"]];
		}
		if (isset($options["dynamic_id"]) && $options["dynamic_id"] != "") {
			$where[] = ["dynamic_id", "=", $options["dynamic_id"]];
		}
		if (isset($options["keyword"]) && $options["keyword"]) {
			$where[] = ["", "exp", \think\Db::raw("(content like '%{$options["keyword"]}%')")];
		}
		$count = \think\Db::name("app_member_dynamic_comment")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_member_dynamic_comment")->where($where)->page($options["page"], $options["limit"])->order("id", "asc")->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_comment_array($item, $options["isAll"] && $options["dynamic_id"] ? $options["sub_comment_length"] : false);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_comment_array($item, $sub_comment_length)
	{
		$user = \think\Db::name("app_member_user")->where(["id" => $item["uid"]])->find();
		$replyList = [];
		$reply_list = $sub_comment_length ? \think\Db::name("app_member_dynamic_comment")->where(["dynamic_id" => $item["dynamic_id"], "pid" => $item["id"]])->limit($sub_comment_length)->order("id", "asc")->select() : [];
		foreach ($reply_list as $reply_item) {
			$replyList[] = self::get_sub_comment_array($reply_item);
		}
		$data = ["id" => $item["id"], "dynamic_id" => $item["dynamic_id"], "status" => $item["status"], "uid" => $item["uid"], "level" => $item["level"], "pid" => $item["pid"], "likeNumber" => $item["likeNumber"], "parent_info" => \think\Db::name("app_member_dynamic_comment")->where(["id" => $item["pid"]])->field("id,content")->find(), "reply_info" => \think\Db::name("app_member_dynamic_comment")->where(["id" => $item["reply_id"]])->field("id,content")->find(), "content" => $item["content"], "addtime" => $item["addtime"], "type" => $item["type"], "user_info" => ["id" => $user["id"], "avatar" => $user["avatar"], "nickname" => $user["nickname"]], "allReply" => \think\Db::name("app_member_dynamic_comment")->where(["dynamic_id" => $item["dynamic_id"], "pid" => $item["id"]])->count(), "replyList" => $replyList];
		return $data;
	}
	public static function get_comment_info($options)
	{
		$item = \think\Db::name("app_member_dynamic_comment")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "ID不存在"];
		}
		$data = self::get_comment_array($item, 0);
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function get_sub_comment_list($options)
	{
		$dynamic = \think\Db::name("app_member_dynamic")->where(["id" => $options["dynamic_id"], "appid" => $options["appid"]])->find();
		if (empty($dynamic)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "动态ID不存在"];
		}
		$comment = \think\Db::name("app_member_dynamic_comment")->where(["dynamic_id" => $options["dynamic_id"], "id" => $options["pid"], "appid" => $options["appid"]])->find();
		if (empty($comment)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "动态ID或评论ID不存在"];
		}
		$where = [];
		$where[] = ["pid", "=", $options["pid"]];
		$where[] = ["dynamic_id", "=", $options["dynamic_id"]];
		$count = \think\Db::name("app_member_dynamic_comment")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_member_dynamic_comment")->where($where)->page($options["page"], $options["limit"])->order("id", "asc")->select();
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_sub_comment_array($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_sub_comment_array($item)
	{
		$user = \think\Db::name("app_member_user")->where(["id" => $item["uid"]])->find();
		$data = ["id" => $item["id"], "dynamic_id" => $item["dynamic_id"], "status" => $item["status"], "uid" => $item["uid"], "level" => $item["level"], "pid" => $item["pid"], "content" => $item["content"], "addtime" => $item["addtime"], "type" => $item["type"], "reply_id" => $item["reply_id"], "user_info" => ["id" => $user["id"], "avatar" => $user["avatar"], "nickname" => $user["nickname"]]];
		if ($item["type"] == 1) {
			$reply_comment = \think\Db::name("app_member_dynamic_comment")->where(["id" => $item["reply_id"]])->find();
			$reply_user = \think\Db::name("app_member_user")->where(["id" => $reply_comment["uid"]])->find();
			$data["reply_info"] = ["comment_info" => ["id" => $reply_comment["id"], "uid" => $reply_comment["uid"], "level" => $reply_comment["level"], "pid" => $reply_comment["pid"], "content" => $reply_comment["content"], "addtime" => $reply_comment["addtime"], "type" => $reply_comment["type"], "reply_id" => $reply_comment["reply_id"]], "reply_user_info" => ["id" => $reply_user["id"], "avatar" => $reply_user["avatar"], "nickname" => $reply_user["nickname"]]];
		}
		return $data;
	}
	public static function dynamic_trials($options)
	{
		\think\Db::startTrans();
		try {
			$dynamic = \think\Db::name("app_member_dynamic")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($dynamic)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "动态ID不存在"];
			}
			if (!in_array($options["status"], [1, 2])) {
				return ["code" => \app\system\model\Code::fail, "msg" => "status错误"];
			}
			if ($dynamic["status"] == $options["status"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "无需审核,已是该状态"];
			}
			$label_list = [];
			if ($options["status"] == 1) {
				preg_match_all("/#([^#]{1,200})#/", $dynamic["content"], $labels, PREG_SET_ORDER);
				if ($labels) {
					\think\Db::name("app_member_dynamic_label_ids")->where(["dynamic_id" => $options["id"]])->delete();
					$label_ids = [];
					foreach ($labels as $label_item) {
						if (isset($label_item[1])) {
							$label_list[] = $label_item[1];
						}
					}
					foreach ($label_list as $label_item) {
						$label = \think\Db::name("app_member_dynamic_label")->where(["appid" => $options["appid"], "name" => $label_item])->find();
						if (empty($label)) {
							$label_id = \think\Db::name("app_member_dynamic_label")->insertGetId(["appid" => $options["appid"], "name" => $label_item, "addtime" => time(), "uid" => $dynamic["uid"]]);
						} else {
							$label_id = $label["id"];
						}
						$label_ids[] = $label_id;
						\think\Db::name("app_member_dynamic_label_ids")->insert(["appid" => $options["appid"], "label_id" => $label_id, "dynamic_id" => $options["id"]]);
					}
					\think\Db::name("app_member_dynamic")->where(["id" => $options["id"]])->update(["label_ids" => implode(",", $label_ids)]);
				}
			}
			\think\Db::name("app_member_dynamic")->where(["id" => $options["id"]])->update(["status" => $options["status"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function comment_trials($options)
	{
		\think\Db::startTrans();
		try {
			$dynamic = \think\Db::name("app_member_dynamic_comment")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($dynamic)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "评论ID不存在"];
			}
			\think\Db::name("app_member_dynamic_comment")->where(["id" => $options["id"]])->update(["status" => $options["status"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function revise_dynamic_info($options)
	{
		\think\Db::startTrans();
		try {
			$dynamic = \think\Db::name("app_member_dynamic")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($dynamic)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "动态ID不存在"];
			}
			if ($options["cid"] && \think\Db::name("app_member_dynamic_classify")->where(["appid" => $options["appid"], "id" => $options["cid"]])->count() == 0) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			$carry_image = 0;
			$carry_video = 0;
			$media_file_ids = $options["media_file_ids"] ?: [];
			foreach ($media_file_ids as $media_file_id) {
				$file = \think\Db::name("public_file_log")->where(["id" => $media_file_id])->find();
				if (!empty($file)) {
					\think\Db::name("public_file_log")->where(["id" => $file["id"]])->update(["overtime" => 0]);
				}
				if ($file["type"] == 0) {
					$carry_image = 1;
				}
				if ($file["type"] == 1) {
					$carry_video = 1;
				}
			}
			if (count($media_file_ids) == 2 && $carry_video && $carry_image) {
				$carry_image = 0;
			}
			\think\Db::name("app_member_dynamic")->where(["id" => $options["id"]])->update(["content" => $options["content"], "title" => $options["title"], "cid" => $options["cid"], "carry_image" => $carry_image, "carry_video" => $carry_video, "hidden_content" => $options["hidden_content"] ?? "", "get_hidden_content_mode" => $options["get_hidden_content_mode"] ?? "", "get_hidden_content_mode_point_quantity" => $options["get_hidden_content_mode_point_quantity"] ?? "", "media_file_ids" => json_encode($media_file_ids)]);
			preg_match_all("/#([^#]{1,200})#/", $options["content"], $labels, PREG_SET_ORDER);
			if ($labels) {
				\think\Db::name("app_member_dynamic_label_ids")->where(["dynamic_id" => $options["id"]])->delete();
				foreach ($labels as $label_item) {
					if (isset($label_item[1])) {
						$label = \think\Db::name("app_member_dynamic_label")->where(["appid" => $options["appid"], "name" => $label_item[1]])->find();
						if (empty($label)) {
							$label_id = \think\Db::name("app_member_dynamic_label")->insertGetId(["appid" => $options["appid"], "name" => $label_item[1], "addtime" => time(), "uid" => $dynamic["uid"]]);
						} else {
							$label_id = $label["id"];
						}
						\think\Db::name("app_member_dynamic_label_ids")->insert(["appid" => $options["appid"], "label_id" => $label_id, "dynamic_id" => $options["id"]]);
					}
				}
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_classify_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%{$options["name"]}%"];
		}
		$count = \think\Db::name("app_member_dynamic_classify")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_member_dynamic_classify")->where($where)->order("sort", "desc")->page($options["page"], $options["limit"])->select();
		$data = [];
		foreach ($list as $item) {
			$item["pid"] = 0;
			$data[] = $item;
		}
		if (!empty($data_structure) && $data_structure == 1) {
			$data = linear_to_tree($data);
		} elseif (!empty($data_structure) && $data_structure == 2) {
			$data = multilevel_linear_sort($data, "|-");
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function add_classify($options)
	{
		\think\Db::startTrans();
		try {
			$level = 0;
			if (!empty($options["pid"])) {
				$row = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["pid"]])->where(["appid" => $options["appid"]])->find();
				if (empty($row)) {
					return ["code" => \app\system\model\Code::fail, "msg" => "父级分类不存在"];
				}
				$level = $row["level"];
			}
			if ($level >= 2) {
				return ["code" => \app\system\model\Code::fail, "msg" => "最多只能添加二级分类"];
			}
			$data = ["appid" => $options["appid"], "name" => $options["name"], "sort" => $options["sort"], "addtime" => time()];
			\think\Db::name("app_member_dynamic_classify")->insert($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_classify_info($options)
	{
		$data = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($data)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function edit_classify($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			$level = 0;
			if (!empty($options["pid"])) {
				$row = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["pid"]])->where(["appid" => $options["appid"]])->find();
				if (empty($row)) {
					return ["code" => \app\system\model\Code::fail, "msg" => "父级分类不存在"];
				}
				$level = $row["level"];
			}
			if ($level >= 2) {
				return ["code" => \app\system\model\Code::fail, "msg" => "最多只能添加二级分类"];
			}
			$data = ["name" => $options["name"], "sort" => $options["sort"]];
			$update = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["id"], "appid" => $options["appid"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_classify($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			$del = \think\Db::name("app_member_dynamic_classify")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_dynamic($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_dynamic")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_dynamic")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_dynamic_comment($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_dynamic_comment")->where(["id" => $options["id"]])->where(["appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_dynamic_comment")->where(["id" => $options["id"]])->delete();
			\think\Db::name("app_member_dynamic_comment")->where(["pid" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_label_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["uid"]) && $options["uid"] != "") {
			$where[] = ["uid", "=", $options["uid"]];
		}
		if (isset($options["keyword"]) && $options["keyword"]) {
			$where[] = ["", "exp", \think\Db::raw("(name like '%{$options["keyword"]}%')")];
		}
		$count = \think\Db::name("app_member_dynamic_label")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$list = \think\Db::name("app_member_dynamic_label")->where($where)->page($options["page"], $options["limit"])->order("browse", "desc")->select();
		$data = [];
		foreach ($list as $item) {
			$item["user_info"] = \think\Db::name("app_member_user")->where(["id" => $item["uid"]])->field("id,nickname,avatar")->find();
			$item["dynamic_count"] = \think\Db::name("app_member_dynamic_label_ids")->where(["label_id" => $item["id"]])->count();
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
}